<html>
<head><meta charset="utf-8"><title>Writing a constant slice with contents in a fixed width? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html">Writing a constant slice with contents in a fixed width?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212867960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212867960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212867960">(Oct 09 2020 at 19:47)</a>:</h4>
<p>What would be the easiest way to write out (to a <code>std::io::Write</code> instance) a compile-time-known constant string that consists of a fixed human-readable string of data (e.g. <code>"abcdefg"</code>) followed by NUL padding out to a fixed width (in this case, 64 bytes)?</p>



<a name="212868088"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212868088" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212868088">(Oct 09 2020 at 19:48)</a>:</h4>
<p>I <em>could</em> write out a constant like <code>[b'a', b'b', b'c', b'd', b'e', b'f', b'g', 0, 0 ,0, ..., 0]</code>, which seems absurd.</p>



<a name="212868141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212868141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212868141">(Oct 09 2020 at 19:49)</a>:</h4>
<p>I don't know of any shorthand syntax for that.</p>



<a name="212868212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212868212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212868212">(Oct 09 2020 at 19:50)</a>:</h4>
<p>I presume you want to avoid the copy? e.g., <code>let mut arr = [0; 64]; arr.copy_from_slice(b"....")</code></p>



<a name="212868255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212868255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212868255">(Oct 09 2020 at 19:50)</a>:</h4>
<p>I wouldn't object to that if I can do it at compile time (such as via const eval).</p>



<a name="212868468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212868468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212868468">(Oct 09 2020 at 19:52)</a>:</h4>
<p>Also, since I'm writing to a <code>std::io::Write</code> instance, I <em>could</em> do something like <code>const S: &amp;[u8]= b"abcdefg"; w.write_all(S); w.write_all(&amp;[0; 64][S.len()..]);</code> , but that similarly seems awkward.</p>



<a name="212868478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212868478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212868478">(Oct 09 2020 at 19:52)</a>:</h4>
<p>Though that's currently the best solution I can think of.</p>



<a name="212868489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212868489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212868489">(Oct 09 2020 at 19:52)</a>:</h4>
<p>format strings can pad arbitrary characters, e.g. <code>"{:\0&lt;64}"</code></p>



<a name="212868557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212868557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212868557">(Oct 09 2020 at 19:53)</a>:</h4>
<p>/me tests to see if that works with <code>std::io::Write</code>...</p>



<a name="212868886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212868886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212868886">(Oct 09 2020 at 19:56)</a>:</h4>
<p>I don't know what that padding translates to in <code>Write</code> calls -- I guess a <code>write_fmt</code> that's expected to deal with it?</p>



<a name="212868974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212868974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212868974">(Oct 09 2020 at 19:57)</a>:</h4>
<p>Interesting, I didn't realize you could do that when using <code>std::io::Write</code> rather than <code>std::fmt::Write</code>.</p>



<a name="212868980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212868980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212868980">(Oct 09 2020 at 19:57)</a>:</h4>
<p>padding is probably not going to optimize away, I suspect</p>



<a name="212868999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212868999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212868999">(Oct 09 2020 at 19:57)</a>:</h4>
<p>Probably not, yeah.</p>



<a name="212869034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869034">(Oct 09 2020 at 19:57)</a>:</h4>
<p>I suspect, though I haven't checked, that the approach of calling write_all with a constant and then with the remaining padding would be faster.</p>



<a name="212869153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869153">(Oct 09 2020 at 19:58)</a>:</h4>
<p>fwiw  <code>v.write_all(b"test000000000000000000000000000000000000000000000000000000000000")</code> compiles to directly loading a pointer into the write call</p>



<a name="212869180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869180">(Oct 09 2020 at 19:59)</a>:</h4>
<p>but I can't get</p>
<div class="codehilite"><pre><span></span><code>pub fn test1(v: &amp;mut dyn std::io::Write) -&gt; std::io::Result&lt;()&gt; {
    let mut buf = [0u8; 64];
    buf[..4].copy_from_slice(b&quot;test&quot;);
    v.write_all(&amp;buf)
}
</code></pre></div>


<p>to optimize away the copy</p>



<a name="212869189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869189">(Oct 09 2020 at 19:59)</a>:</h4>
<p><a href="https://rust.godbolt.org/z/fTfEG4">https://rust.godbolt.org/z/fTfEG4</a></p>



<a name="212869496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869496">(Oct 09 2020 at 20:01)</a>:</h4>
<p>Seems like it might be possible via a <code>const fn</code>, but I'm not sure if it's possible to write that const fn (on stable, or even on nightly).</p>



<a name="212869552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869552">(Oct 09 2020 at 20:02)</a>:</h4>
<p>if the string is a literal, you could <code>&amp;concat!("string", "...64 \0...")[..64]</code></p>



<a name="212869568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869568">(Oct 09 2020 at 20:02)</a>:</h4>
<p>True. :)</p>



<a name="212869581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869581">(Oct 09 2020 at 20:02)</a>:</h4>
<p>You can create the two parts easily enough:</p>
<div class="codehilite"><pre><span></span><code>const X: &amp;str = &quot;foo&quot;;
const Y: &amp;[u8] = &amp;[0u8; 10 - X.len()];
</code></pre></div>



<a name="212869607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869607">(Oct 09 2020 at 20:03)</a>:</h4>
<p>no idea how to concat them in a const fn though</p>



<a name="212869621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869621">(Oct 09 2020 at 20:03)</a>:</h4>
<p><span class="user-mention" data-user-id="295632">@Diggory Blake</span> If I have to write out the <code>64 - X.len()</code> that's no simpler.</p>



<a name="212869661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869661">(Oct 09 2020 at 20:03)</a>:</h4>
<p>there's always a proc-macro possibility too</p>



<a name="212869679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869679">(Oct 09 2020 at 20:03)</a>:</h4>
<p>That's <em>definitely</em> more complicated. :)</p>



<a name="212869803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212869803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212869803">(Oct 09 2020 at 20:05)</a>:</h4>
<p>I think I'm going to stick with this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">        </span><span class="k">const</span><span class="w"> </span><span class="n">S</span>: <span class="kp">&amp;</span><span class="kt">str</span> <span class="o">=</span><span class="w"> </span><span class="s">"the_string"</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">w</span><span class="p">.</span><span class="n">write_all</span><span class="p">(</span><span class="n">S</span><span class="p">.</span><span class="n">as_bytes</span><span class="p">())</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">zero_pad</span><span class="p">(</span><span class="n">w</span><span class="p">,</span><span class="w"> </span><span class="mi">64</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="p">(</span><span class="n">S</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u64</span><span class="p">))</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
</code></pre></div>

<p>(where <code>zero_pad</code> is just a wrapper around "write that many zeroes").</p>



<a name="212870267"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212870267" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212870267">(Oct 09 2020 at 20:10)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">pad64</span><span class="p">(</span><span class="n">input</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">64</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">64</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">64</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">input</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">buf</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="w"></span>
<span class="w">        </span><span class="n">i</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">buf</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="212870428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212870428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212870428">(Oct 09 2020 at 20:11)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">PADDED_FOO</span>: <span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">64</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pad64</span><span class="p">(</span><span class="s">b"foo"</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="212870519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212870519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212870519">(Oct 09 2020 at 20:12)</a>:</h4>
<p>Not bad! Hadn't occurred to me to write out the loop longhand, but since it's const-evaled, it doesn't matter.</p>



<a name="212870545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212870545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212870545">(Oct 09 2020 at 20:12)</a>:</h4>
<p>Is there any way to turn that into a <code>&amp;[u8]</code> rather than a <code>[u8; 64]</code> (where the 64 needs to be known and part of the signature)?</p>



<a name="212870607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212870607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212870607">(Oct 09 2020 at 20:13)</a>:</h4>
<p>It's unfortunate that const eval doesn't seem to be able to return a <code>&amp;'static [u8]</code> from a buf, even though whatever it computes can be known at compile time.</p>



<a name="212870688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212870688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212870688">(Oct 09 2020 at 20:14)</a>:</h4>
<p>you can do it in the constant value, at least:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">PADDED_FOO</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">pad64</span><span class="p">(</span><span class="s">b"foo"</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="212870734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212870734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212870734">(Oct 09 2020 at 20:14)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(min_const_generics)]</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">pad</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">(</span><span class="n">input</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">input</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">buf</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="w"></span>
<span class="w">        </span><span class="n">i</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">buf</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="212870877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212870877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212870877">(Oct 09 2020 at 20:16)</a>:</h4>
<p><span class="user-mention" data-user-id="295632">@Diggory Blake</span> Right, it's definitely possible with const generics. In theory, if that return value could be <code>&amp;[u8]</code>, it wouldn't <em>need</em> const generics, right?</p>



<a name="212871054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212871054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212871054">(Oct 09 2020 at 20:18)</a>:</h4>
<p>Yeah but I think that would go against the way const-eval works: const eval code has to be valid in its own right, so the function could never return a static reference. IIRC you can return an "owned thing" that then devolves into a reference at the const/non-const boundary, but I don't remember the details.</p>



<a name="212871482"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212871482" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212871482">(Oct 09 2020 at 20:22)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">macro_rules</span><span class="o">!</span><span class="w"> </span><span class="n">pad</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="cp">$input</span>:<span class="nc">expr</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">(</span><span class="o">&amp;</span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">64</span><span class="p">];</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">            </span><span class="k">while</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">64</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="cp">$input</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">buf</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="cp">$input</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="w"></span>
<span class="w">                </span><span class="n">i</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="n">buf</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">])</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="n">PADDED_FOO</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pad</span><span class="o">!</span><span class="p">(</span><span class="s">b"foo"</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="212871483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212871483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212871483">(Oct 09 2020 at 20:22)</a>:</h4>
<p>ah so close:</p>



<a name="212871497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212871497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212871497">(Oct 09 2020 at 20:23)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">ops</span>::<span class="n">IndexMut</span><span class="p">;</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">pad</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">IndexMut</span><span class="o">&lt;</span><span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="n">Output</span><span class="o">=</span><span class="kt">u8</span><span class="o">&gt;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Default</span><span class="o">&gt;</span><span class="p">(</span><span class="n">input</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">],</span><span class="w"> </span><span class="n">len</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span>::<span class="n">default</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">len</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">input</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">buf</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="w"></span>
<span class="w">        </span><span class="n">i</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">buf</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>I thought you could do this</p>



<a name="212871517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212871517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212871517">(Oct 09 2020 at 20:23)</a>:</h4>
<p>but trait bounds in const functions are still unstable</p>



<a name="212871634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212871634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212871634">(Oct 09 2020 at 20:24)</a>:</h4>
<p><span class="user-mention" data-user-id="138448">@cuviper</span> that's nice, might make sense to pass 64 in as a macro arg?</p>



<a name="212871696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20a%20constant%20slice%20with%20contents%20in%20a%20fixed%20width%3F/near/212871696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20a.20constant.20slice.20with.20contents.20in.20a.20fixed.20width.3F.html#212871696">(Oct 09 2020 at 20:25)</a>:</h4>
<p>depends whether the goal is to make it configurable or hidden</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>